//
//  ChatUserInfoV.swift
//  Employee
//
//  Created by swordjoy on 2017/3/31.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class ChatUserInfoV: UIView {
  
  var model: ChatUser? {
    
    didSet {
      
      guard let temp = model else { return }
      
      headIconImageV.setImage(with: temp.headIconUrl, placeholder: GlobalConfig.DefaultHeadIconImage)
      nameLabel.text = temp.nickname
      phoneNumberLabel.text = temp.phoneNumber
      departmentLabel.text = temp.department
      
    }
    
  }
  
  var clickHeadIcon: ( (UIImageView) -> Void )?
  
  fileprivate var contentV: UIView!
  
  fileprivate var headIconImageV: UIImageView!
  fileprivate var nameLabel: UILabel!
  fileprivate var phoneNumberLabel: UILabel!
  fileprivate var departmentLabel: UILabel!
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.clear
    
    create()
    
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
}

// MARK: - action

extension ChatUserInfoV {
  
  @objc fileprivate func headIconAction() {
        clickHeadIcon?(headIconImageV)
  }
  
}

// MARK: - fileprivate

extension ChatUserInfoV {
  
  fileprivate func create() {
  
    contentV = UIView()
    addSubview(contentV)
    contentV.backgroundColor = UIColor.white
    contentV.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.top.equalTo(55)
      make.bottom.equalToSuperview()
    }
    
    headIconImageV = UIImageView()
    addSubview(headIconImageV)
    headIconImageV.image = GlobalConfig.DefaultHeadIconImage
    headIconImageV.snp.makeConstraints { (make) in
      make.centerX.equalToSuperview()
      make.top.equalTo(15)
      make.height.width.equalTo(80)
    }
    headIconImageV.layer.cornerRadius = 4
    headIconImageV.clipsToBounds = true
    
    headIconImageV.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self, action: #selector(headIconAction))
    headIconImageV.addGestureRecognizer(tap)
    
    nameLabel = UILabel()
    contentV.addSubview(nameLabel)
    nameLabel.font = GlobalConfig.system22Font
    nameLabel.textColor = GlobalConfig.TextDeepColor
    nameLabel.snp.makeConstraints { (make) in
      make.centerX.equalToSuperview()
      make.top.equalTo(65)
    }
    
    phoneNumberLabel = UILabel()
    contentV.addSubview(phoneNumberLabel)
    phoneNumberLabel.font = GlobalConfig.system16Font
    phoneNumberLabel.textColor = GlobalConfig.TextDeepColor
    phoneNumberLabel.snp.makeConstraints { (make) in
      make.centerX.equalToSuperview()
      make.top.equalTo(nameLabel.snp.bottom).offset(9)
    }
    
    departmentLabel = UILabel()
    contentV.addSubview(departmentLabel)
    departmentLabel.font = GlobalConfig.system15Font
    departmentLabel.textColor = GlobalConfig.TextLightColor
    departmentLabel.snp.makeConstraints { (make) in
      make.centerX.equalToSuperview()
      make.top.equalTo(phoneNumberLabel.snp.bottom).offset(24)
    }
    
  }
  
}
